//
//  HomeHeadView.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/20.
//

#import "HomeHeadView.h"
#import "HomeSleepView.h"
#import "HomeInfoView.h"
#import "HomeFamilyView.h"
#import "HomeSleepDescView.h"

#import <ZZCircleProgress/ZZCircleProgress.h>
@interface HomeHeadView()
@property (nonatomic,strong) UIImageView        * iconView;
@property (nonatomic,strong) UIView             * bgView;
@property (nonatomic,strong) ZZCircleProgress   * progressView;
@property (nonatomic,strong) UIImageView        * infoView;
@property (nonatomic,strong) UIImageView        * typeView;
@property (nonatomic,strong) UILabel            * nameLabel;
@property (nonatomic,strong) UIButton           * infoBtn;
@property (nonatomic,strong) UIImageView        * startView;
@property (nonatomic,strong) UILabel            * startLabel;
@property (nonatomic,strong) UIImageView        * lineView;
@property (nonatomic,strong) UIImageView        * line1View;
@property (nonatomic,strong) UILabel            * timeLabel;

@property (nonatomic,strong) UIImageView        * endView;
@property (nonatomic,strong) UILabel            * endLabel;

@property (nonatomic,strong) HomeInfoView       * homeInfoView;
@property (nonatomic,strong) HomeFamilyView     * familyView;
@property (nonatomic,strong) HomeSleepView      * sleepView;
@property (nonatomic,strong) HomeSleepDescView  * descView;
@end

@implementation HomeHeadView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
    }
    return self;
}

-(void)setSubViewLayout{
    
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.iconView];
    [self addSubview:self.bgView];
    [self addSubview:self.progressView];
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(230 * FMScaleX, 230 * FMScaleX));
        make.centerX.equalTo(@0);
        make.top.equalTo(self.iconView.mas_top).offset(50 * FMScaleX);
    }];
    [self addSubview:self.infoView];
    [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(206 * FMScaleX, 103 * FMScaleX));
        make.centerX.equalTo(@0);
        make.top.equalTo(self.iconView.mas_top).offset(68 * FMScaleX);
    }];
    [self addSubview:self.typeView];
    [_typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(88 * FMScaleX, 63 * FMScaleX));
        make.centerX.equalTo(@0);
        make.top.equalTo(self.infoView.mas_top).offset(18 * FMScaleX);
    }];
    [self addSubview:self.nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.top.equalTo(self.typeView.mas_bottom).offset(7 * FMScaleX);
    }];
    [self addSubview:self.infoBtn];
    [_infoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(13 * FMScaleX, 13 * FMScaleX));
        make.centerY.equalTo(self.nameLabel);
        make.left.equalTo(self.nameLabel.mas_right).offset(4 * FMScaleX);
    }];
    
    [self addSubview:self.startView];
    [_startView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(20 * FMScaleX, 20 * FMScaleX));
        make.left.mas_offset(27 * FMScaleX);
        make.top.equalTo(self.iconView.mas_centerY).offset(15 * FMScaleX);
    }];
    [self addSubview:self.startLabel];
    [_startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.startView);
        make.left.equalTo(self.startView.mas_right).offset(8 * FMScaleX);
        make.width.mas_offset( 50 * FMScaleX);
    }];
    [self addSubview:self.endView];
    [_endView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(20 * FMScaleX, 20 * FMScaleX));
        make.right.mas_offset(-27 * FMScaleX);
        make.top.equalTo(self.iconView.mas_centerY).offset(15 * FMScaleX);
    }];
    [self addSubview:self.endLabel];
    [_endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.endView);
        make.right.equalTo(self.endView.mas_left).offset(-8 * FMScaleX);
        make.width.mas_offset( 50 * FMScaleX);
    }];
    [self addSubview:self.timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.centerY.equalTo(self.startView);
    }];
    [self addSubview:self.lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.endView);
        make.height.mas_offset(1 * FMScaleX);
        make.width.mas_offset( 44 * FMScaleX);
        make.left.equalTo(self.startLabel.mas_right).offset(0 * FMScaleX);
    }];
    [self addSubview:self.line1View];
    [_line1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.equalTo(self.lineView);
        make.right.equalTo(self.endLabel.mas_left).offset(0 * FMScaleX);
    }];
    [self addSubview:self.sleepView];
    [_sleepView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.startView.mas_bottom).offset(10 * FMScaleX);
        make.height.mas_offset(85 * FMScaleX);
    }];
    
//    [self addSubview:self.familyView];
//    [_familyView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(@0);
//        make.top.equalTo(self.sleepView.mas_bottom).offset(10 * FMScaleX);
//        make.height.mas_offset(80 * FMScaleX);
//    }];
//    
    [self addSubview:self.homeInfoView];
    [_homeInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.sleepView.mas_bottom).offset(10 * FMScaleX);
        make.height.mas_offset(ISUPDATE ? 130 * FMScaleX : 250 * FMScaleX);
    }];
    
    [self addGradientLayer:self.bgView];
    [self creatRotationAnimation];
    [self pauseRotationAnimation];
}

-(void)setInfoModel:(HomeSleepModel *)infoModel{
    
    [self.sleepView setInfoModel:infoModel];
    [self.homeInfoView setInfoModel:infoModel];
    self.startLabel.text = infoModel.startTime.length > 0 ? infoModel.startTime : @"--";
    self.endLabel.text = infoModel.endTime.length> 0 ? infoModel.endTime : @"--";
    self.progressView.progress = infoModel.healthValue / 100;
    self.timeLabel.text = infoModel.date;
    if (infoModel.healthValue < 50) {
        self.typeView.image = [UIImage imageNamed:@"Home_he_type_hong"];
    }else if (infoModel.healthValue > 50 && infoModel.healthValue < 70){
        self.typeView.image = [UIImage imageNamed:@"Home_he_type_huang"];
    }else if (infoModel.healthValue > 69 && infoModel.healthValue < 85){
        self.typeView.image = [UIImage imageNamed:@"Home_he_type_lan"];
    }else{
        self.typeView.image = [UIImage imageNamed:@"Home_he_type_lv"];
    }
}

-(void)addGradientLayer:(UIView *)view{
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = view.bounds;
    UIColor *startColor = RDRGBA(0xF5F2ED, 0);
    UIColor *middleColor = RDRGBA(0xF5F2ED, 1);
    UIColor *endColor = RDRGBA(0xF5F2ED, 1);
    gradientLayer.colors = @[(id)startColor.CGColor, (id)middleColor.CGColor, (id)endColor.CGColor];
    gradientLayer.locations = @[@0.0, @0.5, @1.0];
    gradientLayer.startPoint = CGPointMake(0.5, 0.0);
    gradientLayer.endPoint = CGPointMake(0.5, 1.0);
    [view.layer addSublayer:gradientLayer];
    
}

-(void)creatRotationAnimation{
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = @(0);
    rotationAnimation.toValue = @(M_PI * 2);
    rotationAnimation.duration = 4.0;
    rotationAnimation.repeatCount = HUGE_VALF;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    [self.iconView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    NSLog(@"Starting button animation");

}

-(void)startRotationAnimation{
    
    [self.iconView.layer removeAnimationForKey:@"rotationAnimation"];

}

-(void)endRotationAnimation{

    CFTimeInterval pausedTime = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    self.iconView.layer.speed = 0.0;
    self.iconView.layer.timeOffset = pausedTime;

}

- (void)pauseRotationAnimation {
    CFTimeInterval pausedTime = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    self.iconView.layer.speed = 0.0;
    self.iconView.layer.timeOffset = pausedTime;
}

- (void)resumeRotationAnimation {
    CFTimeInterval pausedTime = self.iconView.layer.timeOffset;
    self.iconView.layer.speed = 1.0;
    self.iconView.layer.timeOffset = 0.0;
    self.iconView.layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    self.iconView.layer.beginTime = timeSincePause;
}

-(void)infoBtnAction{
    
    [self.descView startAnimation];
    
}

-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(20 * FMScaleX, 10 * FMScaleX, 335 * FMScaleX, 335 * FMScaleX)];
        _iconView.image = [UIImage imageNamed:@"Home_he_icon"];
    }
    return _iconView;
}

-(UIView *)bgView{
    
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(20 * FMScaleX, 345 * FMScaleX - 300 * FMScaleX, 335 * FMScaleX, 290 * FMScaleX)];
        _bgView.backgroundColor = UIColor.clearColor;
    }
    return _bgView;
}
-(ZZCircleProgress *)progressView{
    
    if(!_progressView){
        _progressView = [[ZZCircleProgress alloc]initWithFrame:CGRectZero pathBackColor:RDRGB(0xEBE3DA) pathFillColor:RDRGB(0xAD7D62) startAngle:180 strokeWidth:10];
        _progressView.reduceAngle = 180;
        _progressView.showPoint = NO;
        _progressView.showProgressText = NO;
    }
    return _progressView;
}
-(UIImageView *)infoView{
    
    if (!_infoView) {
        _infoView = [[UIImageView alloc]init];
        _infoView.image = [UIImage imageNamed:@"Home_he_info"];
    }
    return _infoView;
}
-(UIImageView *)typeView{
    
    if (!_typeView) {
        _typeView = [[UIImageView alloc]init];
        _typeView.image = [UIImage imageNamed:@"Home_he_type"];
    }
    return _typeView;
}
-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = Medium(14);
        _nameLabel.textColor = RDRGB(0x666666);
        _nameLabel.text = @"睡眠健康";
    }
    return _nameLabel;
}
-(UIButton *)infoBtn{
    
    if (!_infoBtn) {
        _infoBtn = [[UIButton alloc]init];
        [_infoBtn setImage:[UIImage imageNamed:@"Home_he_info_icon"] forState:UIControlStateNormal];
        [_infoBtn addTarget:self action:@selector(infoBtnAction ) forControlEvents:UIControlEventTouchUpInside];
    }
    return _infoBtn;
}

-(UIImageView *)startView{
    
    if (!_startView) {
        _startView = [[UIImageView alloc]init];
        _startView.image = [UIImage imageNamed:@"Home_sleep_start"];
    }
    return _startView;
}
-(UILabel *)startLabel{
    
    if (!_startLabel) {
        _startLabel = [[UILabel alloc]init];
        _startLabel.font = Regular(16);
        _startLabel.textColor = UIColor.blackColor;
        _startLabel.text = @"--";
        _startLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _startLabel;
}
-(UIImageView *)lineView{
    
    if (!_lineView) {
        _lineView = [[UIImageView alloc]init];
        _lineView.image = [UIImage imageNamed:@"Home_sleep_line"];
    }
    return _lineView;
}

-(UIImageView *)line1View{
    
    if (!_line1View) {
        _line1View = [[UIImageView alloc]init];
        _line1View.image = [UIImage imageNamed:@"Home_sleep_line"];
    }
    return _line1View;
}

-(UILabel *)timeLabel{
    
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = Bold(15);
        _timeLabel.textColor = RDRGB(0xAD7D62);
    }
    return _timeLabel;
}

-(UIImageView *)endView{
    
    if (!_endView) {
        _endView = [[UIImageView alloc]init];
        _endView.image = [UIImage imageNamed:@"Home_sleep_end"];
    }
    return _endView;
}
-(UILabel *)endLabel{
    
    if (!_endLabel) {
        _endLabel = [[UILabel alloc]init];
        _endLabel.font = Regular(16);
        _endLabel.textColor = UIColor.blackColor;
        _endLabel.text = @"--";
        _endLabel.textAlignment = NSTextAlignmentRight;
    }
    return _endLabel;
}
-(HomeSleepView *)sleepView{
    
    if (!_sleepView) {
        _sleepView = [[HomeSleepView alloc]init];
    }
    return _sleepView;
}
-(HomeInfoView *)homeInfoView{
    
    if (!_homeInfoView) {
        _homeInfoView = [[HomeInfoView alloc]init];
    }
    return _homeInfoView;
}
-(HomeFamilyView *)familyView{
    
    if (!_familyView) {
        _familyView = [[HomeFamilyView alloc]init];
    }
    return _familyView;
}

-(HomeSleepDescView *)descView{
    
    if (!_descView) {
        _descView = [[HomeSleepDescView alloc]initWithFrame:[UIViewController currentViewController].view.bounds];
        [FMKeyWindow addSubview:_descView];
    }
    return _descView;
}
@end
